import java.util.Arrays;

/**
 * 模板方法设计模式
 */
public class TemplateTest {
    public static void main(String[] args) {
        Timer timer = new Timer() {
            @Override
            public void code() {
                int[] result = PrimeNumber.getPrimeNumbers(10000);
                System.out.println(Arrays.toString(result));
            }
        };
        long spendTime = timer.getTime();
        System.out.println("Use time: " + spendTime);
    }

}

class PrimeNumber {

    public static int[] getPrimeNumbers(int max) {
        int[] arr = new int[max];
        arr[0] = 2;
        int count = 1;
        for (int i = 3; i <= max; i += 2) {
            boolean flag = true;
            for (int j = 3; j <= Math.sqrt(i); j += 2) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                arr[count++] = i;
            }
        }
        int[] result = new int[count];
        for (int i = 0; i < count; i++) {
            result[i] = arr[i];
        }
        return result;
    }
}


abstract class Timer {
    public long getTime() {
        long startTime = System.currentTimeMillis();
        this.code();
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
    }

    abstract public void code();

}
